package online.flowerinsnow.minecraft.bukkit.testffa.runnable;

import online.flowerinsnow.minecraft.bukkit.testffa.core.TestFFACore;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class FakeGlassRunnable implements Runnable {
    public static Map<Player, Set<Location>> fakeBlocks = new HashMap<>();
    @SuppressWarnings("deprecation")
    @Override
    public void run() {
        Bukkit.getOnlinePlayers().forEach(p -> {
            fakeBlocks.getOrDefault(p, new HashSet<>()).forEach(l -> {
                if (!(TestFFACore.getCombatManager().getCombatTarget(p) != null && l.distance(p.getLocation()) < 5 &&
                        l.getX() <= 16 && l.getX() >= -16 && l.getZ() <= 16 && l.getZ() >= -16)) {
                    p.sendBlockChange(l, Material.AIR, (byte) 0);
                }
            });
            fakeBlocks.put(p, new HashSet<>());
            if (TestFFACore.getCombatManager().getCombatTarget(p) != null) {
                for (int x = -5; x < 6; x++) {
                    for (int y = -5; y < 6; y++) {
                        for (int z = -5; z < 6; z++) {
                            Location location = p.getLocation().add(x, y, z);
                            Block block = location.getBlock();
                            if (block.getType() == Material.AIR &&
                                    block.getX() <= 16 && block.getX() >= -16 && block.getZ() <= 16 && block.getZ() >= -16) {
                                fakeBlocks.get(p).add(location);
                                p.sendBlockChange(location, Material.STAINED_GLASS, (byte) 14);
                            }
                        }
                    }
                }
            }
        });
    }
}
